<?php

namespace app\admin\controller;

use app\admin\model\Config;
use app\admin\model\System as SystemModel;
use app\BaseController;

class System extends BaseController
{
    /* 列表 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $data = SystemModel::order('id asc')->select();
        return show(config('status.success'), '获取列表', $data);
    }

    /* 添加 */
    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $name = input('name', '');
        $tel = input('tel', '');
        $map = input('map', '');
        $status = input('status', 0, 'intval');

        $result = SystemModel::create([
            'name' => $name,
            'tel' => $tel,
            'map' => $map,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '添加成功');
        } else {
            return show(config('status.error_insert'), '添加失败');
        }
    }

    /* 获取 */
    public function get()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $result = [
            'price' => Config::where('key', 'price')->value('value'),
            'phone' => Config::where('key', 'phone')->value('value'),
            'about' => Config::where('key', 'about')->value('value'),
            'file_ext' => Config::where('key', 'file_ext')->value('value'),
            'file_size' => Config::where('key', 'file_size')->value('value'),
            'sm_share_title' => Config::where('key', 'sm_share_title')->value('value'),
            'sm_share_pic' => Config::where('key', 'sm_share_pic')->value('value'),
        ];
        return show(config('status.success'), '查询成功',  $result);
    }

    /* 修改 */
    public function modifySystem()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $price = input('price', 0, 'intval');
        Config::where([
            'key' => 'price',
        ])->update([
            'value' => $price
        ]);
        Config::where([
            'key' => 'phone',
        ])->update([
            'value' => request()->post('phone'),
        ]);

        Config::where([
            'key' => 'about',
        ])->update([
            'value' => request()->post('about'),
        ]);

        Config::where([
            'key' => 'file_ext',
        ])->update([
            'value' => request()->post('file_ext'),
        ]);

        Config::where([
            'key' => 'file_size',
        ])->update([
            'value' => request()->post('file_size'),
        ]);

        Config::where([
            'key' => 'sm_share_title',
        ])->update([
            'value' => request()->post('sm_share_title'),
        ]);

        Config::where([
            'key' => 'sm_share_pic',
        ])->update([
            'value' => request()->post('sm_share_pic'),
        ]);

        return show(config('status.success'), '修改成功');
    }

    /* 修改 */
    public function modifyWechat()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $appid = input('appid', '');
        $appsecret = input('appsecret', '');

        $result = SystemModel::update([
            'appid' => $appid,
            'appsecret' => $appsecret
        ], ['id' => $cid]);
        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }
}
